package online.heycm.platform.log;

import online.heycm.platform.common.entity.constant.Constant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.function.Consumer;

/**
 * @author hey
 * @version 1.0
 * @date 2023/12/27 9:39
 */
public class Logger {
    private final org.slf4j.Logger logger;

    Logger(org.slf4j.Logger logger) {
        this.logger = logger;
    }

    private void log(Consumer<org.slf4j.Logger> l) {
        l.accept(logger);
        incrStepId();
    }

    private void incrStepId() {
        String stepId = MDC.get(Constant.STEP_ID);
        if (StringUtils.isNotBlank(stepId)) {
            int i = Integer.parseInt(stepId);
            MDC.put(Constant.STEP_ID, (i + 1) + "");
        }
    }

    public void trace(String format, Object... arguments) {
        if (logger.isTraceEnabled()) {
            log(l -> l.trace(format, arguments));
        }
    }

    public void debug(String format, Object... arguments) {
        if (logger.isDebugEnabled()) {
            log(l -> l.debug(format, arguments));
        }
    }

    public void info(String format, Object... arguments) {
        if (logger.isInfoEnabled()) {
            log(l -> l.info(format, arguments));
        }
    }

    public void warn(String format, Object... arguments) {
        if (logger.isWarnEnabled()) {
            log(l -> l.warn(format, arguments));
        }
    }

    public void error(String format, Object... arguments) {
        if (logger.isErrorEnabled()) {
            log(l -> l.error(format, arguments));
        }
    }

}
